/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-15 15:08:50
 * @LastEditors: huangtianyang
 * @LastEditTime: 2024-05-11 18:26:33
 * @FilePath: \driving-training-admin\src\views\classManagement\class\edit\index.ts
 */
import { reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import { data } from './formConfig'
import { fileListProps, fileProps, formDataInterface, PropsFormpData } from '@/utils/interface'
import { addClass, classDetailed, classOperationInterface, classTagListInterface, editClass } from '@/apis/class'
import router from '@/router'
import { routerStore } from '@/store/router.store'
import { ElMessage } from 'element-plus'
import { userStore } from '@/store/user.store'
import { addDisplayBlockToImages } from '@/utils'

interface tagInterface {
    label: string
    describe: string
    id?: string
}

export default class ClassOpreation {
    formData = reactive<PropsFormpData[]>([...data])
    //编辑类型 create | edit | look
    operation = reactive({
        type: 'create',
        id: ''
    })
    uStore = userStore()
    routerStore = routerStore()
    constructor() {
        const route = useRoute()
        this.operation.type = route.query.type as string
        this.operation.id = route.query?.id as string
        console.log('route', route)

        const options: { label: string; value: string }[] = []
        this.uStore.queryDrivingSchoolSimple((res: Map<string, any>) => {
            res.forEach(item => {
                options.push({ label: item.shortName, value: item.id })
            })

            this.formData.forEach((item: PropsFormpData, index: number) => {
                if (Object.prototype.toString.call(item) === '[object Array]' && index === 0) {
                    const childList = item as formDataInterface[]
                    childList[0].options = options
                }
            })
        })

        if (this.operation.type !== 'create') {
            this.getData()
        }
    }
    getData = (arg?: any) => {
        classDetailed(this.operation.id).then(res => {
            if (Number(res.data.code) === 200) {
                const result = res.data.result

                this.formData.forEach((child: PropsFormpData) => {
                    if (Object.prototype.toString.call(child) === '[object Array]') {
                        const childList = child as formDataInterface[]
                        childList.forEach((item: formDataInterface) => {
                            if (item.key === 'className') {
                                item.value = result.className
                            } else if (item.key === 'vehicleType') {
                                item.value = result.vehicleType + ''
                            } else if (item.key === 'classType') {
                                item.value = result.classType + ''
                            } else if (item.key === 'signUpMsg') {
                                item.value = result.signUpMsg
                            } else if (item.key === 'classFeatureList') {
                                item.value = result.classFeatureList
                            } else if (item.key === 'advertisePic') {
                                item.value = [{ tag: result.advertiseTag, key: result.advertisePic }]
                            } else if (item.key === 'schoolId') {
                                item.value = result.schoolId
                            } else if (item.key === 'classNumber') {
                                item.value = result.classNumber
                            } else if (item.key === 'classTag') {
                                item.value = result.classTag as string[]
                            } else if (item.key === 'classExplain') {
                                const arr: { label: string; describe: string }[] = []
                                result.classExplain.forEach((item: any) => {
                                    arr.push({
                                        label: item.tag,
                                        describe: item.msg
                                    })
                                })

                                item.value = arr
                            } else if (item.key === 'classVideoPic') {
                                //班别视频及展示图
                                const imageArray: fileProps[] = []
                                const videoArray: fileProps[] = []

                                result.classPic.forEach((f: any, index: number) => {
                                    imageArray.push({
                                        index,
                                        tag: f.tag,
                                        type: 'image',
                                        list: [
                                            {
                                                key: f.pic,
                                                name: f.tag
                                            }
                                        ]
                                    })
                                })
                                result.classVideo.forEach((f: any, index: number) => {
                                    videoArray.push({
                                        index: Number(item.imageCount) + index,
                                        tag: '',
                                        type: 'video',
                                        list: []
                                    })
                                    if (f.cover) {
                                        videoArray[index].list.push({
                                            key: f.cover,
                                            name: f.tag,
                                            type: 'image'
                                        })
                                        videoArray[index].list.push({
                                            key: f.video,
                                            name: f.tag,
                                            type: 'video'
                                        })
                                    }
                                })

                                item.value = new Array(Number(item.imageCount) + Number(item.videoCount))
                                item.value.splice(0, imageArray.length, ...imageArray)
                                item.value.splice(Number(item.imageCount), videoArray.length, ...videoArray)
                                console.log('班别视频及展示图e', item)
                            } else if (item.key === 'classIntroduction') {
                                //0'课程内容', '费用清单', '学习流程', '退款说明', '班别视频介绍'

                                //内容
                                const contentArray: string[] = [
                                    result.courseContent,
                                    result.feeList,
                                    result.studyFlow,
                                    result.refundExplain,
                                    result.classVideoIntroduce
                                ]
                                item.content = contentArray
                                item.value = '课程内容'
                                item.index = 0
                            }
                        })
                    }
                })
            }
        })
    }
    onCancel = () => {
        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
        router.push('/class')
    }
    //保存
    onSave = (formRef: any) => {
        const data: classOperationInterface = {
            classExplain: [],
            className: '',
            classNumber: '',
            classPic: [],
            classTag: [],
            classVideo: [],
            classVideoIntroduce: '',
            courseContent: '',
            feeList: '',
            finishCycle: 0,
            refundExplain: '',
            schoolId: '',
            signUpMsg: '',
            studyFlow: ''
        }

        if (formRef?.validateForm()) {
            this.formData.forEach((child: PropsFormpData) => {
                if (Object.prototype.toString.call(child) === '[object Array]') {
                    const childList = child as formDataInterface[]
                    childList.forEach((item: formDataInterface) => {
                        console.log('item', item)
                        if (item.key === 'schoolId') {
                            data.schoolId = item.value as string
                        } else if (item.key === 'classNumber') {
                            data.classNumber = item.value as string
                        } else if (item.key === 'className') {
                            data.className = item.value as string
                        } else if (item.key === 'signUpMsg') {
                            data.signUpMsg = item.value as string
                        } else if (item.key === 'classTag') {
                            data.classTag = item.value as string[]
                        } else if (item.key === 'classExplain') {
                            const tagList = item.value as { label: string; value: string }[]
                            const arr: classTagListInterface[] = []
                            tagList.forEach((tag: any) => {
                                arr.push({
                                    msg: tag.describe,
                                    tag: tag.label
                                })
                            })
                            data.classExplain = [...arr]
                        } else if (item.key === 'classVideoPic') {
                            //班别视频及展示图
                            const arr: fileProps[] = item.value as fileProps[]
                            const classPic: classTagListInterface[] = []
                            const classVideo: classTagListInterface[] = []
                            arr.forEach(item => {
                                if (item.type === 'video') {
                                    let cover = ''
                                    let video = ''
                                    let tag = ''
                                    item.list.forEach((list: fileListProps) => {
                                        console.log('list', list)
                                        if (list?.type === 'video') {
                                            video = list.key
                                            tag = list.name
                                        } else {
                                            cover = list.key
                                        }
                                    })
                                    classVideo.push({
                                        cover,
                                        video,
                                        tag
                                    })
                                } else {
                                    item.list.forEach((list: fileListProps) => {
                                        console.log('list', list)
                                        classPic.push({ pic: list.key, tag: list.name })
                                    })
                                }
                            })
                            data.classPic = classPic
                            //驾校视频
                            data.classVideo = classVideo
                        } else if (item.key === 'classIntroduction') {
                            //驾校介绍说明
                            const contentArray: string[] = item.content as string[]
                            //课程内容
                            data.courseContent = addDisplayBlockToImages(contentArray[0])
                            //费用清单
                            data.feeList = addDisplayBlockToImages(contentArray[1])
                            //学习流程
                            data.studyFlow = addDisplayBlockToImages(contentArray[2])
                            //退款说明
                            data.refundExplain = addDisplayBlockToImages(contentArray[3])
                            //班别视频介绍
                            data.classVideoIntroduce = addDisplayBlockToImages(contentArray[4])
                        }
                    })
                }
            })

            console.log('data', data)
            if (this.operation.type === 'create') {
                addClass(data).then(res => {
                    if (Number(res.data.code) === 200) {
                        ElMessage.success('新增成功！')
                        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
                        router.push('/class')
                    }
                })
            } else {
                data.id = this.operation.id
                editClass(data).then(res => {
                    if (Number(res.data.code) === 200) {
                        ElMessage.success('修改成功！')
                        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
                        router.push('/class')
                    }
                })
            }
        }
    }
}
